13. Code Coverage
Code Coverage
ND079 JPND C3 L4 A11 Code Coverage
Testing the Tests
Tests find bugs in our application, but what finds bugs in our tests? **Code Coverage **is one tool for helping to evaluate your tests for problems. It measures what percentage of your application is executed by unit tests. You can run tests for code coverage in IntelliJ by right-clicking on a test method or class and selecting Run With Coverage.
Example Class and Test
Given the below class and test methods, code coverage would identify which lines of the class were not tested.
FizzBuzzService.java
public class FizzBuzzService {
/**
* Returns "Fizz" for multiples of 3, "Buzz" for multiples of 5,
* and "FizzBuzz" for multiples of 3 and 5.
* @param i
* @return
*/
public String fizzBuzz(int i) {
if(i % 15 == 0) {
return "FizzBuzz";
} else if (i % 3 == 0) {
return "Fizz";
} else if (i % 5 == 0) {
return "Buzz";
} else {
return "" + i;
}
}
}
FizzBuzzServiceTest.java
class FizzBuzzServiceTest {
private FizzBuzzService fizzBuzzService;
@BeforeEach
void init() {
fizzBuzzService = new FizzBuzzService();
}
@ParameterizedTest
@ValueSource(ints={3,6,9,33})
public void fizzBuzz_multipleThreeNotFive_returnsFizz(int n) {
assertEquals("Fizz", fizzBuzzService.fizzBuzz(n));
}
@ParameterizedTest
@ValueSource(ints={5,10,20,55})
public void fizzBuzz_multipleFiveNotThree_returnsBuzz(int n) {
assertEquals("Buzz", fizzBuzzService.fizzBuzz(n));
}
@ParameterizedTest
@ValueSource(ints={15,225,3375})
public void fizzBuzz_multipleThreeAndFive_returnsFizzBuzz(int n) {
assertEquals("FizzBuzz", fizzBuzzService.fizzBuzz(n));
}
}
If you run the above test for coverage, you will identify that one line of the FizzBuzzService was not tested.
return "" + i;
That's because we never tested numbers that are not multiples of 3 or 5. By adding an additional test, we would receive 100% coverage.
@ParameterizedTest
@ValueSource(ints={1,2,41,43})
public void fizzBuzz_notMultipleOfThreeOrFive_returnsNumber(int n) {
assertEquals(Integer.toString(n), fizzBuzzService.fizzBuzz(n));
}
How Much Coverage
Code coverage is simply a tool to help you find flaws in your tests. Some methods do not require tests, such as accessor methods (getters/setters) or default constructors. Writing unnecessary tests adds noise and slows down your test suite, so an arbitrary coverage goal is counterproductive. Use coverage to help you make sure your requirements are tested.